library(MASS)
library(ggplot2)
library(manysvms)
library(SupportVectorLab)
source("bls_bsh_tsvm.R")
source("cL2p_ls_tsvm.R")
source("closs_tbsvm.R")
source("Synt-1-Helper.R")
save_path <- "../../paper/"
height    <- 5
width     <- 5


set.seed(1234)
n <- 100
x11 <- runif(n, -10, 10)
x12 <- -2*x11 - 10 + rnorm(n, 0, 1)
x21 <- runif(n, -10, 10)
x22 <- -2*x21 + 10 + rnorm(n, 0, 1)

X1 <- cbind(x11, x12)
X2 <- cbind(x21, x22)
X <- rbind(X1, X2)
Xno <- mvrnorm(5, mu = c(-20, -20), Sigma = diag(1, nrow = 2))
yno <- rep(2, 5)
y <- rep(c(1, 2), c(n, n))

X <- rbind(X, Xno)
y <- c(y, yno)

# X <- apply(X, 2, scale)

dataXy <- data.frame(cbind(X, y))
colnames(dataXy) <- c("X1", "X2", "y")
dataXy$y <- as.factor(dataXy$y)

max.steps <- 8000000
irls.steps <- 500
cccp.steps <- 500
hq.steps <- 500
eps <- 1e-5
eps.cccp <- 0
eps.hq <- 0
eps.irls <- 0


model1 <- bls_bsh_tsvm(X, y, C1 = 1, C2 = 1, 
                       lambda1 = 2, lambda2 = 2,
                       eps = eps, max.steps = max.steps, 
                       cccp.steps = cccp.steps, eps.cccp = eps.cccp)
model2 <- hinge_tsvm(X, y, C1 = 1, C2 = 1, eps = eps, max.steps = max.steps)
model3 <- cL2p_ls_tsvm(X, y, C1 = 1, C2 = 1, 
                       p = 1.5, epsilon1 = 1, 
                       irls.steps = irls.steps, eps.irls = eps.irls)
model4 <- ls_tsvm(X, y, C1 = 1, C2 = 1)
model5 <- sh_tsvm(X, y, C1 = 1, C2 = 1, eps = eps, max.steps = max.steps)
model6 <- closs_tbsvm(X, y, lambda1 = 1, lambda2 = 1,
                      sigma1 = 0.5, sigma2 = 0.5,
                      hq.steps = hq.steps, eps.hq = eps.hq)

plotF1(model1)
fig_path <- paste(save_path, "Synt1-BLS-BSH-TSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

plotF1(model2)
fig_path <- paste(save_path, "Synt1-Hinge-TSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

plotF1(model3)
fig_path <- paste(save_path, "Synt1-CL2p-LS-TSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

plotF1(model4)
fig_path <- paste(save_path, "Synt1-LS-TSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

plotF1(model5)
fig_path <- paste(save_path, "Synt1-SH-TSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

plotF1(model6)
fig_path <- paste(save_path, "Synt1-Closs-TBSVM-2.eps", sep = "")
ggsave(fig_path, height = height, width = width)

accuracy(y, predict(model1, X))
accuracy(y, predict(model2, X))
accuracy(y, predict(model3, X))
accuracy(y, predict(model4, X))
accuracy(y, predict(model5, X))
accuracy(y, predict(model6, X))
